package com.kt.dao.imp;

import com.kt.dao.UserDAO;
import com.kt.domain.User;
import com.kt.utils.UtilDB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImp implements UserDAO {

    private User selectUser(PreparedStatement preparedStatement, Connection connection) {
        User user = null;

        ResultSet resultSet = null;
        try {

            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                user = new User();
                user.setUid(resultSet.getInt("uid"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setName(resultSet.getString("name"));
                user.setLevel(resultSet.getInt("level"));
                user.setSex(resultSet.getString("sex"));
                user.setPhone(resultSet.getString("phone"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
            UtilDB.close(connection);
        }
        return user;
    }
    public User findUser(String username, String password) {
        Connection connection = UtilDB.getConnection();

        PreparedStatement preparedStatement = null;
        String sql = "select * from user where username=? and password = ?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return selectUser(preparedStatement, connection);
    }

    public User findByUsername(String username) {
        Connection connection = UtilDB.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "select * from user where username=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return selectUser(preparedStatement, connection);
    }

    public User findByPhone(String phone) {
        Connection connection = UtilDB.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "select * from user where phone=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, phone);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return selectUser(preparedStatement, connection);
    }

    public User findByUid(String uid) {
        Connection connection = UtilDB.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "select * from user where uid=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, uid);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return selectUser(preparedStatement, connection);
    }

    public int changeUserInfo(User newUser) {
        Connection connection = UtilDB.getConnection();
        PreparedStatement preparedStatement = null;
        int result = 0;
        String sql = "update user set username=?, password=?, name=?, sex=?, phone=? where uid=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, newUser.getUsername());
            preparedStatement.setString(2, newUser.getPassword());
            preparedStatement.setString(3, newUser.getName());
            preparedStatement.setString(4, newUser.getSex());
            preparedStatement.setString(5, newUser.getPhone());
            preparedStatement.setInt(6, newUser.getUid());
            result = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            UtilDB.close(connection);
        }
        return result;
    }

    public int deleteUser(String username) {
        Connection connection = UtilDB.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "delete from user where username = ?";
        int result = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            UtilDB.close(connection);
        }
        return result;
    }

    public int addUser(User user) {
        int result = 0;
        Connection connection = UtilDB.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "insert into user values(null, ?, ?, ?, ?, ?, ?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getName());
            preparedStatement.setString(4, user.getSex());
            preparedStatement.setString(5, user.getPhone());
            preparedStatement.setInt(6, user.getLevel());
            result = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return result;
    }


}
